import mechanize
from BeautifulSoup import *

def calculate(br, grossMargin, escalator, initialPayment, instThisYear):
    url = "https://tools.cleanpowerfinance.com/quoting/financingcomparison/validate/on_success_action/analyze"
    data = "is_primary=0"
    data += "&margin=" + grossMargin
    data += "&proposal_id=165641"
    data += "&financing_product_id=20"
    data += "&id="
    data += "&install_this_year=" + instThisYear
    data += "&down_payment=" + initialPayment
    data += "&annual_increase=" + escalator
    data += "&billing_surcharge=1"
    response = br.open(url, data)
    html = response.read()
    soup = BeautifulSoup(html)
    table = soup.find("table")
    data = []
    trs = table.findAll('tr')
    for x in xrange(len(trs)):
        col = trs[x].findAll('td')
        colVal = col[0].findAll('b')
        if len(colVal)==0:
            data.append(col[0].string.replace("&nbsp;",""))
        else:
            data.append(colVal[0].string)
    return data
    #for elt in data:
    #    print elt

#br = mechanize.Browser(factory=mechanize.RobustFactory())
#br.set_handle_robots(False)
#br.open("https://tools.cleanpowerfinance.com/user/login")
#br.select_form("loginform")
#br["username"] = "kate@completesolar.com"
#br["password"] = "solar123solar"
#response = br.submit()

#calculate(br, '21', '3.5', '1000', '1')
